package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AjglJctzsqcEntity;
import cn.com.css.taxInspection.attention.entity.ClcfjdbbEntity;
import cn.com.css.taxInspection.attention.mapper.ClcfjdbbMapper;
import cn.com.css.taxInspection.attention.service.ClcfjdbbService;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ClcfjdbbServiceImpl extends ServiceImpl<ClcfjdbbMapper, ClcfjdbbEntity> implements ClcfjdbbService {


    @Resource
    private DmGySwjgService dmGySwjgService;
    @Resource
    private DmGySwryService dmGySwryService;
    @Resource
    private HttpSessionService httpSessionService;

    @Override
    public IPage<ClcfjdbbEntity> queryClcfjdbbTable(ClcfjdbbEntity clcfjdbbEntity) throws ParseException {
//        String currentUserId = httpSessionService.getCurrentUserId();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<ClcfjdbbEntity> page = new Page<>(clcfjdbbEntity.getPage(), clcfjdbbEntity.getLimit());
        LambdaQueryWrapper<ClcfjdbbEntity> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(ClcfjdbbEntity::getSwjgDm, currentUserId);
        //案件编号
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getJcajbh())) {
            queryWrapper.like(ClcfjdbbEntity::getJcajbh, clcfjdbbEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getNsrsbh())) {
            queryWrapper.like(ClcfjdbbEntity::getNsrsbh, clcfjdbbEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getNsrmc())) {
            queryWrapper.like(ClcfjdbbEntity::getNsrmc, clcfjdbbEntity.getNsrmc());
        }
        //是否出两书
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getSfcls())) {
            queryWrapper.eq(ClcfjdbbEntity::getSfcls, clcfjdbbEntity.getSfcls());
        }
        //立案日期
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getStartTime())) {
            queryWrapper.between(ClcfjdbbEntity::getLarq, sdf.parse(clcfjdbbEntity.getStartTime()), sdf.parse(clcfjdbbEntity.getEndTime()));
        }
        queryWrapper.eq(ClcfjdbbEntity::getTbbz, clcfjdbbEntity.getTbbz());
        queryWrapper.orderByDesc(ClcfjdbbEntity::getJcajbh);
        IPage<ClcfjdbbEntity> resPage = page(page, queryWrapper);
        List<ClcfjdbbEntity> clcfjdbbEntityList = page.getRecords();
        for (ClcfjdbbEntity entity : clcfjdbbEntityList) {
            entity.setAjly("金三");
        }
        return resPage;
    }

    @Override
    public List<ClcfjdbbEntity> exportClcfjdbbTable(ClcfjdbbEntity clcfjdbbEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String currentUserId = httpSessionService.getCurrentUserId();

        LambdaQueryWrapper<ClcfjdbbEntity> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(ClcfjdbbEntity::getSwjgDm, currentUserId);

        //案件编号
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getJcajbh())) {
            queryWrapper.like(ClcfjdbbEntity::getJcajbh, clcfjdbbEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getNsrsbh())) {
            queryWrapper.like(ClcfjdbbEntity::getNsrsbh, clcfjdbbEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getNsrmc())) {
            queryWrapper.like(ClcfjdbbEntity::getNsrmc, clcfjdbbEntity.getNsrmc());
        }
        //是否出两书
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getSfcls())) {
            queryWrapper.eq(ClcfjdbbEntity::getSfcls, clcfjdbbEntity.getSfcls());
        }
        //立案日期
        if (TsBqUtils.isNotNull(clcfjdbbEntity.getStartTime())) {
            queryWrapper.between(ClcfjdbbEntity::getLarq, sdf.parse(clcfjdbbEntity.getStartTime()), sdf.parse(clcfjdbbEntity.getEndTime()));
        }
        queryWrapper.eq(ClcfjdbbEntity::getTbbz, clcfjdbbEntity.getTbbz());
        queryWrapper.orderByDesc(ClcfjdbbEntity::getJcajbh);
        return list(queryWrapper);
    }
}
